#include "CharacterEditor.h"

bool CharacterEditor::OnEvent(const SEvent& event) {
	if(mEnv==NULL)
		return false;
	
	if (event.EventType == EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();
		IGUIEnvironment* mEnv = Context.device->getGUIEnvironment();
		
		switch(event.GUIEvent.EventType)
		{
		
		/*
		If a scrollbar changed its scroll position, and it is
		'our' scrollbar (the one with id GUI_ID_TRANSPARENCY_SCROLL_BAR), then we change
		the transparency of all gui elements. This is a very
		easy task: There is a skin object, in which all color
		settings are stored. We simply go through all colors
		stored in the skin and change their alpha value.
		*/
		case EGET_SCROLL_BAR_CHANGED:
			if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				
				for (u32 i=0; i<EGDC_COUNT ; ++i)
				{
					SColor col = mEnv->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
					col.setAlpha(pos);
					mEnv->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
				}
				
			}
			break;
		
		/*
		If a button was clicked, it could be one of 'our'
		three buttons. If it is the first, we shut down the engine.
		If it is the second, we create a little window with some
		text on it. We also add a string to the list box to log
		what happened. And if it is the third button, we create
		a file open dialog, and add also this as string to the list box.
		That's all for the event receiver.
		*/
		case EGET_BUTTON_CLICKED:
			switch(id)
			{
			case GUI_ID_QUIT_BUTTON:
				Context.device->closeDevice();
				return true;
		
			case GUI_ID_NEW_WINDOW_BUTTON:
				{
				Context.listbox->addItem(L"Window created");
				Context.counter += 30;
				if (Context.counter > 200)
					Context.counter = 0;
		
				IGUIWindow* window = mEnv->addWindow(
					rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
					false, // modal?
					L"Test window");
		
				mEnv->addStaticText(L"Please close me",
					rect<s32>(35,35,140,50),
					true, // border?
					false, // wordwrap?
					window);
				}
				return true;
		
			case GUI_ID_FILE_OPEN_BUTTON:
				Context.listbox->addItem(L"File open");
				mEnv->addFileOpenDialog(L"Please choose a file.");
					return true;
		
				default:
					return false;
				}
				break;
		
			default:
				break;
			}
		}
	return false;
}

/*
 * Creating the receiver
 * 	
	// Then create the event receiver, giving it that context structure.
	MyEventReceiver receiver(context);

	// And tell the device to use our custom event receiver.
	device->setEventReceiver(&receiver);
*/

bool CharacterEditor::init(IrrlichtDevice* device) {
	IGUIEnvironment* mEnv = device->getGUIEnvironment();

	/*
	To make the font a little bit nicer, we load an external font
	and set it as the new default font in the skin.
	To keep the standard font for tool tip text, we set it to
	the built-in font.
	*/

	IGUISkin* skin = mEnv->getSkin();
	IGUIFont* font = mEnv->getFont("fonts/fonthaettenschweiler.bmp");
	if (font)
		skin->setFont(font);

	skin->setFont(mEnv->getBuiltInFont(), EGDF_TOOLTIP);

	/*
	We add three buttons. The first one closes the engine. The second
	creates a window and the third opens a file open dialog. The third
	parameter is the id of the button, with which we can easily identify
	the button in the event receiver.
	*/	

	mEnv->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
			L"Quit", L"Exits Program");
	mEnv->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
			L"New Window", L"Launches a new Window");
	mEnv->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
			L"File Open", L"Opens a file");

	/*
	Now, we add a static text and a scrollbar, which modifies the
	transparency of all gui elements. We set the maximum value of
	the scrollbar to 255, because that's the maximal value for
	a color value.
	Then we create an other static text and a list box.
	*/

	mEnv->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
	IGUIScrollBar* scrollbar = mEnv->addScrollBar(true,
			rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
	scrollbar->setMax(255);

	// set scrollbar position to alpha value of an arbitrary element
	scrollbar->setPos(mEnv->getSkin()->getColor(EGDC_WINDOW).getAlpha());

	mEnv->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
	IGUIListBox * listbox = mEnv->addListBox(rect<s32>(50, 140, 250, 210));
	mEnv->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));

	// Store the appropriate data in	§ a context structure.
	SAppContext context;
	context.device = device;
	context.counter = 0;
	context.listbox = listbox;
	
	return true;
}
